#include "httpresponse.h"
#include "loger.h"
HttpResponse::HttpResponse(QObject * parent):QObject (parent)
{
    responseHeader.insert("Server","QtWebServer");
}
HttpResponse::~HttpResponse(){
    QString info("HttpResponse已析构");
    Loger::info(info);
}

/**
 * 添加响应头
 * @brief addHeader
 * @param name
 * @param value
 */
void HttpResponse::addHeader(QString& name, QString& value){
    responseHeader.insert(name, value);
}

/**
 * 响应客户端
 * @brief write
 * @param responseBody
 * @param len
 */
void HttpResponse::write(QByteArray * responseBody, int len){
    QString s;
    //响应行
    QString line("HTTP/1.0 ");
    line.append(QString::number(statusCode));
    line.append("\r\n");
    s.append(line);
    //响应头
    responseHeader.insert("Content-Length",QString::number(len));
    QString header;
    QMap<QString, QString>::iterator it;
    for (it = responseHeader.begin();it != responseHeader.end(); it++) {
          header.append(it.key());
          header.append(": ");
          header.append(it.value());
          header.append("\r\n");
    }
    header.append("\r\n");
    s.append(header);
    QByteArray res = s.toUtf8().data();
    //响应体
    res.append(responseBody->begin());
    conn->write(res);
}

